package com.neu.wangweili.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "W_CookieServlet", urlPatterns = {"/W_CookieServlet"})
public class W_CookieServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Cookie []cookies=request.getCookies();
        if (cookies.length==1){
            response.sendRedirect("MgrLogin/mgr_login.jsp");

        }else{
            Cookie stu=null;
            Cookie stuBoss=null;
            Cookie mgr=null;
            for (Cookie cook:cookies){
                if(cook.getName().equals("stu_info")){
                    stu=cook;
                    break;
                }
            }
            if(stu!=null){
                String value=stu.getValue();
                String[] values=value.split(":");
                request.getRequestDispatcher("StuLoginServlet?no="+values[0]+"&pwd="+values[1]).forward(request,response);
            }else{
               response.sendRedirect("MgrLogin/mgr_login.jsp");
            }

        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
